package com.lmy.springCloud.contorller;

import com.lmy.springCloud.entity.Member;
import com.lmy.springCloud.service.MemberService;
import com.lmy.springCloud.utils.Result;
import com.lmy.springCloud.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class MemberController {

    @Value(value = "${spring.application.name}")
    private String serverName;

    @Value(value = "${server.port}")
    private String serverPort;

    @Resource
    private MemberService memberService;

    @RequestMapping(value = "/getMemberById/{id}", method = RequestMethod.GET)
    public Result<Member> getMemberById(@PathVariable(value = "id") Long id) {
        Member member = memberService.queryMemberById(id);
        return member != null ? Result.success(member, serverName + "-" + serverPort) : Result.info(ResultEnum.QUERY_NONE);
    }

    @RequestMapping(value = "/saveMember", method = RequestMethod.POST)
    public Result saveMember(@RequestBody Member member) {
        log.info("saveMember: " + member);
        int affected = memberService.saveMember(member);
        return affected > 0 ? Result.info(ResultEnum.SAVE_OK) : Result.info(ResultEnum.SAVE_ERROR);
    }

}